<template>
  <div class="user-sidebar">
    <el-card class="sidebar-card">
      <template #header>
        <div class="sidebar-header">
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </div>
      </template>
      
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        router
        @select="handleMenuSelect"
      >
        <el-menu-item index="/user/list">
          <el-icon><UserFilled /></el-icon>
          <span>用户列表</span>
        </el-menu-item>
        
        <el-menu-item index="/user/role">
          <el-icon><Lock /></el-icon>
          <span>角色管理</span>
        </el-menu-item>
        
        <el-menu-item index="/user/permission">
          <el-icon><Key /></el-icon>
          <span>权限管理</span>
        </el-menu-item>
        
        <el-menu-item index="/user/department">
          <el-icon><OfficeBuilding /></el-icon>
          <span>部门管理</span>
        </el-menu-item>
        
        <el-menu-item index="/user/log">
          <el-icon><Document /></el-icon>
          <span>操作日志</span>
        </el-menu-item>
      </el-menu>
      
      <!-- 统计信息 -->
      <div class="sidebar-stats">
        <el-divider>统计信息</el-divider>
        <div class="stat-item">
          <span class="stat-label">总用户数</span>
          <span class="stat-value">{{ stats.totalUsers }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">在线用户</span>
          <span class="stat-value online">{{ stats.onlineUsers }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">今日新增</span>
          <span class="stat-value">{{ stats.todayNewUsers }}</span>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { 
  User, 
  UserFilled, 
  Lock, 
  Key, 
  OfficeBuilding, 
  Document 
} from '@element-plus/icons-vue'

const route = useRoute()

// 统计信息
const stats = ref({
  totalUsers: 0,
  onlineUsers: 0,
  todayNewUsers: 0
})

// 当前激活的菜单
const activeMenu = computed(() => {
  return route.path
})

// 菜单选择处理
const handleMenuSelect = (index) => {
  console.log('选择菜单:', index)
}

// 获取统计信息
const fetchStats = async () => {
  try {
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 500))
    
    stats.value = {
      totalUsers: 1247,
      onlineUsers: 89,
      todayNewUsers: 23
    }
  } catch (error) {
    console.error('获取统计信息失败:', error)
  }
}

onMounted(() => {
  fetchStats()
})
</script>

<style scoped>
.user-sidebar {
  width: 260px;
  min-width: 260px;
  margin-right: 20px;
}

.sidebar-card {
  height: 100%;
}

.sidebar-header {
  display: flex;
  align-items: center;
  font-weight: 600;
  color: #303133;
}

.sidebar-header .el-icon {
  margin-right: 8px;
  color: #409EFF;
}

.sidebar-menu {
  border: none;
}

.sidebar-menu .el-menu-item {
  height: 48px;
  line-height: 48px;
  margin: 4px 0;
  border-radius: 6px;
}

.sidebar-menu .el-menu-item.is-active {
  background-color: #ecf5ff;
  color: #409EFF;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #f5f7fa;
}

.sidebar-stats {
  margin-top: 20px;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.stat-item:last-child {
  border-bottom: none;
}

.stat-label {
  font-size: 14px;
  color: #606266;
}

.stat-value {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.stat-value.online {
  color: #67C23A;
}

:deep(.el-divider__text) {
  font-size: 12px;
  color: #909399;
  background-color: #fff;
}
</style>